package com.nativetalk.controller.app.four.teacher.record;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.bean.teacher.TeacherRecordUploadRequest;
import com.nativetalk.common.Constants;
import com.nativetalk.common.Lang;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.live.insert.LiveInsertService;
import com.nativetalk.service.teacher.record.TeacherRecordService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 老师上课录音
 * Created by Peter on 2017-03-09
 */
@Controller
public class FourTeacherRecordController {

    private static Logger logger = Logger.getLogger(FourTeacherRecordController.class);
    @Resource
    private TeacherRecordService teacherRecordService;

    @Resource
    private LiveInsertService liveInsertService;

    /**
     * @return RetInfo
     * @Purpose 老师上传录音
     * @version 4.0
     * @time 2016-11-1
     * @author lizhun
     */
    @RequestMapping(value = "/app/11/teatoken/recording/upload", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public RetInfo uploadRecording(@ModelAttribute TeacherRecordUploadRequest uploadRequest,
                                   @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.error(Constants.prefix_out + JsonUtil.toJson(uploadRequest) + "token:" + token);

            //校验token，如果校验通过就从缓存中读取Teacher
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("token is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.EN);
                logger.info("缓存中没有teatoken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtTeacherResult.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.EN);
                logger.info("缓存的token不是TdNtTeacherResult类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) o;

            RetInfo retInfo = new RetInfo();
            if (uploadRequest.getRecordFile() == null) {
                retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
                retInfo.setTip("The parameter 'recordFile' is required");
                return retInfo;
            }
            if (uploadRequest.getRoomId() == null || uploadRequest.getRoomId().intValue() == 0) {
                retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
                retInfo.setTip("The parameter 'roomId' is required");
                return retInfo;
            }

            retInfo = teacherRecordService.uploadRecording(tdNtTeacherResult.getTea_id(), uploadRequest);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }

    /**
     * @return RetInfo
     * @Purpose 老师上传直播课录音
     * @version 4.1
     * @time 2017-03=15
     * @author Peter
     */
    @RequestMapping(value = "/app/11/teatoken/live/recording/upload", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public RetInfo uploadLiveRecording(@ModelAttribute TeacherRecordUploadRequest uploadRequest,
                                       @RequestHeader(value = "live_cou_his_id", required = false) String live_cou_his_id) {
        try {
            //打印入参
            logger.error(Constants.prefix_out + JsonUtil.toJson(uploadRequest) + "live_cou_his_id:" + live_cou_his_id);


            RetInfo retInfo = new RetInfo();
            if (uploadRequest.getRecordFile() == null) {
                retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
                retInfo.setTip("文件不能为空");
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            if (live_cou_his_id == null || "".equals(live_cou_his_id.trim())) {
                retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
                retInfo.setTip("live_cou_his_id不能为空");
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            int liveCouHisId = Integer.parseInt(live_cou_his_id);
            retInfo = liveInsertService.uploadLiveCourseHisRecording(liveCouHisId, uploadRequest.getRecordFile());

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

}


